// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.reports;

import org.dengues.commons.IDenguesCoreContants;
import org.dengues.core.DenguesCorePlugin;
import org.dengues.designer.ui.DesignerUIPlugin;
import org.dengues.reports.editor.ReportDesignMultiEditor;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IPerspectiveDescriptor;
import org.eclipse.ui.IPerspectiveListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;

/**
 * The activator class controls the plug-in life cycle
 */
public class DenguesReportsPlugin extends AbstractUIPlugin {

    // The plug-in ID
    public static final String PLUGIN_ID = "org.dengues.reports"; //$NON-NLS-1$

    // The shared instance
    private static DenguesReportsPlugin plugin;

    /**
     * The constructor
     */
    public DenguesReportsPlugin() {
        plugin = this;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
     */
    @Override
    public void start(BundleContext context) throws Exception {
        super.start(context);
        addPartListener();
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
     */
    @Override
    public void stop(BundleContext context) throws Exception {
        plugin = null;
        super.stop(context);
    }

    /**
     * Returns the shared instance
     * 
     * @return the shared instance
     */
    public static DenguesReportsPlugin getDefault() {
        return plugin;
    }

    public void addPartListener() {
        final IWorkbenchWindow activeWorkbenchWindow = DesignerUIPlugin.getDefault().getActiveWorkbenchWindow();
        if (activeWorkbenchWindow != null) {
            activeWorkbenchWindow.getPartService().addPartListener(new IPartListener() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partActivated(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partActivated(IWorkbenchPart part) {
                    if (part instanceof ReportDesignMultiEditor) {
                        ReportDesignMultiEditor editor = (ReportDesignMultiEditor) part;
                        activeWorkbenchWindow.getShell().setText(editor.getDesignEditor().getReportTitle());
                    }

                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partBroughtToTop(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partBroughtToTop(IWorkbenchPart part) {
                    if (part instanceof ReportDesignMultiEditor) {
                        ReportDesignMultiEditor editor = (ReportDesignMultiEditor) part;
                        activeWorkbenchWindow.getShell().setText(editor.getDesignEditor().getReportTitle());
                    }
                }

                public void partClosed(IWorkbenchPart part) {
                    boolean openDenguesEditor = DesignerUIPlugin.getDefault().isOpenDenguesEditor();
                    if (!openDenguesEditor) {
                        activeWorkbenchWindow.getShell().setText(DenguesCorePlugin.getDefault().getDenguesTitle("")); //$NON-NLS-1$
                    }
                    if (activeWorkbenchWindow.getActivePage() != null) {
                        IViewPart findView = activeWorkbenchWindow.getActivePage().findView(
                                IDenguesCoreContants.VIEW_COMPONENT_PROPERTY_ID);
                        if (findView != null) {
                            // if (part instanceof ProcessMultiEditor || part instanceof DatabaseMultiEditor) {
                            // ((ComponentPropertyView) findView).clearAll();
                            // }
                        }
                    }
                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partDeactivated(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partDeactivated(IWorkbenchPart part) {

                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPartListener#partOpened(org.eclipse.ui.IWorkbenchPart)
                 */
                public void partOpened(IWorkbenchPart part) {
                }

            });
            activeWorkbenchWindow.addPerspectiveListener(new IPerspectiveListener() {

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPerspectiveListener#perspectiveActivated(org.eclipse.ui.IWorkbenchPage,
                 * org.eclipse.ui.IPerspectiveDescriptor)
                 */
                public void perspectiveActivated(IWorkbenchPage page, IPerspectiveDescriptor perspective) {
                    activeWorkbenchWindow.getShell().setText(DenguesCorePlugin.getDefault().getDenguesTitle("")); //$NON-NLS-1$
                }

                /*
                 * (non-Javadoc)
                 * 
                 * @see org.eclipse.ui.IPerspectiveListener#perspectiveChanged(org.eclipse.ui.IWorkbenchPage,
                 * org.eclipse.ui.IPerspectiveDescriptor, java.lang.String)
                 */
                public void perspectiveChanged(IWorkbenchPage page, IPerspectiveDescriptor perspective, String changeId) {

                }
            });
        }
    }
}
